package com.huilian.iotbox.mqtt.server.base.service.impl;

import com.huilian.iotbox.data.dto.CommodityStockRecordDto;
import com.huilian.iotbox.data.dto.CommodityStockRecordItemDto;
import com.huilian.iotbox.data.dto.DisplayRackItemDto;
import com.huilian.iotbox.data.enums.CommodityStockRecordFirstTypeEnum;
import com.huilian.iotbox.data.enums.CommodityStockRecordSecondTypeEnum;
import com.huilian.iotbox.data.po.DisplayRackItem;
import com.huilian.iotbox.data.po.GiftIncrement;
import com.huilian.iotbox.data.service.ErrorLogCommonService;
import com.huilian.iotbox.data.utils.Util;
import com.huilian.iotbox.data.vo.DisplayRackItemVo;
import com.huilian.iotbox.mqtt.server.base.dao.DisplayRackItemDao;
import com.huilian.iotbox.mqtt.server.base.service.CommodityStockRecordItemService;
import com.huilian.iotbox.mqtt.server.base.service.CommodityStockRecordService;
import com.huilian.iotbox.mqtt.server.base.service.DisplayRackItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DisplayRackItemServiceImpl implements DisplayRackItemService {

    @Resource
    private DisplayRackItemDao displayRackItemDao;
    @Autowired
    private ErrorLogCommonService errorLogCommonService;
    @Resource
    private CommodityStockRecordService commodityStockRecordService;
    @Resource
    private CommodityStockRecordItemService commodityStockRecordItemService;
    @Override
    public DisplayRackItemVo findDisplayRackItemById(DisplayRackItemDto displayRackItemDto) {
        return displayRackItemDao.findDisplayRackItemById(displayRackItemDto);
    }

    @Override
    public int updateOneById(DisplayRackItemVo displayRackItemVo) {
        return displayRackItemDao.updateOneById(displayRackItemVo);
    }

    @Override
    public List<DisplayRackItemVo> findDisplayRackItemByDeviceId(DisplayRackItemDto displayRackItemDto) {
        return displayRackItemDao.findDisplayRackItemByDeviceId(displayRackItemDto);
    }
    @Override
    public Integer subStockById(DisplayRackItemVo displayRackItemVo, int i, GiftIncrement giftIncrement) {
        DisplayRackItemDto displayRackItemDto = new DisplayRackItemDto();
        displayRackItemDto.setId(displayRackItemVo.getId());
        DisplayRackItemVo oldDisplayRackItemVo = displayRackItemDao.findDisplayRackItemById(displayRackItemDto);
        int stockBefore = oldDisplayRackItemVo.getExistStock();
        int existStock = oldDisplayRackItemVo.getExistStock() - displayRackItemVo.getExistStock();
        oldDisplayRackItemVo.setExistStock(existStock);
        giftIncrement.setTuiLiAfterStock(existStock);

        int flag = displayRackItemDao.updateOneById(oldDisplayRackItemVo);
        if (flag == 0) {
            if (i > 5) {
                errorLogCommonService.writerInfoErrorLog("退礼减库存失败！", oldDisplayRackItemVo);
            } else {
                i++;
                this.subStockById(displayRackItemVo, i, giftIncrement);
            }
        } else {
            //生成出库记录
            CommodityStockRecordDto addCommodityStockRecordDto = new CommodityStockRecordDto();
            addCommodityStockRecordDto.setUserId(displayRackItemVo.getUserId());
            addCommodityStockRecordDto.setMerchantStoreId(displayRackItemVo.getMerchantStoreId());
            addCommodityStockRecordDto.setDeviceId(displayRackItemVo.getDeviceId());
            addCommodityStockRecordDto.setOutTradeNo(Util.getOutTradeNo());
            addCommodityStockRecordDto.setDescription("设备出礼");
            addCommodityStockRecordDto.setOutDisplayRackId(displayRackItemVo.getDisplayRackId());
            addCommodityStockRecordDto.setFirstType(CommodityStockRecordFirstTypeEnum.OUTBOUND.getCode());
            addCommodityStockRecordDto.setSecondaryType(CommodityStockRecordSecondTypeEnum.DEVICE_GIFT_OUT.getCode());
            addCommodityStockRecordDto.setOperationUserId(displayRackItemVo.getUserId());
            addCommodityStockRecordDto.setCreateTime(new Date());
            commodityStockRecordService.add(addCommodityStockRecordDto);

            CommodityStockRecordItemDto commodityStockRecordItemDto = new CommodityStockRecordItemDto();
            commodityStockRecordItemDto.setCommodityStockRecordId(addCommodityStockRecordDto.getId());
            commodityStockRecordItemDto.setCommodityId(displayRackItemVo.getCommodityId());
            commodityStockRecordItemDto.setQuantity(displayRackItemVo.getTuiLiIncrement());
            commodityStockRecordItemDto.setHierarchy(displayRackItemVo.getHierarchy());
            commodityStockRecordItemDto.setFreightLane(displayRackItemVo.getFreightLane());
            commodityStockRecordItemDto.setCostPrice(displayRackItemVo.getCostPrice());
            commodityStockRecordItemDto.setRetailPrice(displayRackItemVo.getRetailPrice());
            commodityStockRecordItemDto.setStockBefore(stockBefore);
            commodityStockRecordItemDto.setStockAfter(existStock);
            commodityStockRecordItemDto.setCostPriceBefore(displayRackItemVo.getCostPrice());
            commodityStockRecordItemDto.setCostPriceAfter(displayRackItemVo.getCostPrice());
            commodityStockRecordItemDto.setRetailPriceBefore(displayRackItemVo.getRetailPrice());
            commodityStockRecordItemDto.setRetailPriceAfter(displayRackItemVo.getRetailPrice());
            commodityStockRecordItemService.add(commodityStockRecordItemDto);
        }
        return flag;
    }

    @Override
    public DisplayRackItemVo findDisplayRackItemByHierarchyAndFreightLane(DisplayRackItemDto displayRackItemDto) {
        return displayRackItemDao.findDisplayRackItemByHierarchyAndFreightLane(displayRackItemDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer editVendMachStock(DisplayRackItemDto displayRackItemDto,Integer shipmentFailureNum,int i){
        DisplayRackItemVo displayRackItemVo  = displayRackItemDao.findDisplayRackItemByHierarchyAndFreightLane(displayRackItemDto);
        int stockBefore = displayRackItemVo.getStock();
        int existStock = displayRackItemVo.getStock() + shipmentFailureNum;
        displayRackItemVo.setStock(existStock);
        int flag =  displayRackItemDao.updateOneById(displayRackItemVo);
        if (flag == 0) {
            if (i > 5) {
                errorLogCommonService.writerInfoErrorLog("退礼减库存失败！", displayRackItemVo);
            } else {
                i++;
                this.editVendMachStock(displayRackItemDto, shipmentFailureNum, i);
            }
        } else {
//            //生成出库记录  启文说出货失败不记录的
//            CommodityStockRecordDto addCommodityStockRecordDto = new CommodityStockRecordDto();
//            addCommodityStockRecordDto.setUserId(displayRackItemVo.getUserId());
//            addCommodityStockRecordDto.setMerchantStoreId(displayRackItemVo.getMerchantStoreId());
//            addCommodityStockRecordDto.setDeviceId(displayRackItemVo.getDeviceId());
//            addCommodityStockRecordDto.setOutTradeNo(Util.getOutTradeNo());
//            addCommodityStockRecordDto.setDescription("售货机出货失败");
//            addCommodityStockRecordDto.setInDisplayRackId(displayRackItemVo.getDisplayRackId());
//            addCommodityStockRecordDto.setFirstType(CommodityStockRecordFirstTypeEnum.INTO_STOCK_ORDER.getCode());
//            addCommodityStockRecordDto.setSecondaryType(CommodityStockRecordSecondTypeEnum.VENDING_MACHINE_OUT.getCode());
//            addCommodityStockRecordDto.setOperationUserId(displayRackItemVo.getUserId());
//            addCommodityStockRecordDto.setCreateTime(new Date());
//            commodityStockRecordService.add(addCommodityStockRecordDto);
//
//            CommodityStockRecordItemDto commodityStockRecordItemDto = new CommodityStockRecordItemDto();
//            commodityStockRecordItemDto.setCommodityStockRecordId(addCommodityStockRecordDto.getId());
//            commodityStockRecordItemDto.setCommodityId(displayRackItemVo.getCommodityId());
//            commodityStockRecordItemDto.setQuantity(shipmentFailureNum);
//            commodityStockRecordItemDto.setCostPrice(displayRackItemVo.getCostPrice());
//            commodityStockRecordItemDto.setRetailPrice(displayRackItemVo.getRetailPrice());
//            commodityStockRecordItemDto.setStockBefore(stockBefore);
//            commodityStockRecordItemDto.setStockAfter(existStock);
//            commodityStockRecordItemDto.setCostPriceBefore(displayRackItemVo.getCostPrice());
//            commodityStockRecordItemDto.setCostPriceAfter(displayRackItemVo.getCostPrice());
//            commodityStockRecordItemDto.setRetailPriceBefore(displayRackItemVo.getRetailPrice());
//            commodityStockRecordItemDto.setRetailPriceAfter(displayRackItemVo.getRetailPrice());
//            commodityStockRecordItemService.add(commodityStockRecordItemDto);
        }
        return flag;
    }

    @Override
    public List<DisplayRackItem> findAllDisplayRackItemByCondition(DisplayRackItem displayRackItemCondition) {
        return displayRackItemDao.findAllDisplayRackItemByCondition(displayRackItemCondition);
    }

    @Override
    public void insertToList(List<DisplayRackItem> addList) {
        displayRackItemDao.insertToList(addList);
    }

    @Override
    public void updateToList(List<DisplayRackItem> updateList) {
        displayRackItemDao.updateToList(updateList);
    }

    @Override
    public void deleteByBatch(Long displayRackId, Map<Integer, Integer> map) {
        displayRackItemDao.deleteByBatch(displayRackId, map);
    }

    @Override
    public void updateStatus(DisplayRackItemDto displayRackItemDto) {
        displayRackItemDao.updateStatus(displayRackItemDto);
    }

    @Override
    public void updateStock(DisplayRackItemDto displayRackItemDto) {
        displayRackItemDao.updateStock(displayRackItemDto);
    }

    @Override
    public int numberOfFailures(DisplayRackItemDto displayRackItemDto) {
        return displayRackItemDao.numberOfFailures(displayRackItemDto);
    }

}
